package cn.houjunxiong.plugins.vp;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;

import org.json.JSONException;

import java.util.List;

@CapacitorPlugin(name = "VideoPlayer")
public class VideoPlayerPlugin extends Plugin {

    @PluginMethod
    public void echo(PluginCall call) {
        VideoPlayer implementation = new VideoPlayer(getContext());
        String value = call.getString("value");

        JSObject ret = new JSObject();
        ret.put("value", implementation.echo(value));
        call.resolve(ret);
    }

    @PluginMethod
    public void play(PluginCall call) throws JSONException {
        VideoPlayer implementation = new VideoPlayer(getContext());
        JSObject ret = new JSObject();
        String key = "value";
        List<VideoPlayer.Video> videos = implementation.getVideos(call.getArray("videos"));
        Integer index = call.getInt("defaultIndex");
        if (index != null && videos != null && !videos.isEmpty()) {
            boolean res = implementation.playByMXPlayer(videos, index);
            ret.put(key, res);
        } else {
            ret.put(key, false);
        }
        call.resolve(ret);
    }
}
